package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserMapper;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {

    @Override
    public List<User> findAll() {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = userMapper.findAll();
        // 释放资源
        MyBatisUtils.release(sqlSession);
        return list;
    }

    @Override
    public PageInfo<User> findByPage(Integer pageNum, Integer pageSize) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.findAll();

        // 释放资源
        MyBatisUtils.release(sqlSession);
        return new PageInfo<>(list);
    }

    @Override
    public void save(User user) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        System.out.println("qqqqqqqqqqqqqqqqq主键返回之前的user"+user);
        userMapper.save(user);
        System.out.println("hhhhhhhhhhhhhh主键返回之后的user"+user);
        //获取要添加的角色的id数组
        Integer[] ids = user.getIds();
        Integer uid= user.getId();
        for (Integer rid : ids) {
            userMapper.saveUser_Role(uid,rid);
        }

        //这里需要设置下主键返回策略
        // 释放资源
        MyBatisUtils.release(sqlSession);
    }

    @Override
    public User findById(Integer id) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.findById(id);
        // 释放资源
        MyBatisUtils.release(sqlSession);
        return user;
    }

    @Override
    public void update(User user) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //更新用户信息
        System.out.println("要更新的用户数据是"+user.toString());
        userMapper.update(user);
        //根据用户id删除之前所有的角色user.getid
        Integer userid = user.getId();
        System.out.println("要删除的用户id是+"+userid);
        //修改之前先删除 用户角色
        userMapper.updateDeleteRole(userid);
        //重新添加用户角色
        Integer[] ids = user.getIds();
        if(ids!=null){
            for (Integer id : ids) {
                userMapper.saveUser_Role(userid,id);
            }
        }



        // 释放资源
        MyBatisUtils.release(sqlSession);
    }

    @Override
    public void delete(Integer id) {
        // 创建代理
        SqlSession sqlSession = MyBatisUtils.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //删除用户之前需要把外键删除  不然报错 所以要先删除中间表的外键

        //  根据用户id删除 中间表的数据
        userMapper.updateDeleteRole(id);
        //删除用户
        userMapper.delete(id);
        // 释放资源
        MyBatisUtils.release(sqlSession);
    }
}
